/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.*;
import static jsp2php.Translation.makeConsistent;

class StaticHttpServletResponseTranslator extends TranslatorBase {

   static JavaType javaType;

   StaticHttpServletResponseTranslator() {
      try {
         fields.put( "SC_ACCEPTED",                         JavaType.value_translator( "int" ).literal( "202" ) );
         fields.put( "SC_BAD_GATEWAY",                      JavaType.value_translator( "int" ).literal( "502" ) );
         fields.put( "SC_BAD_REQUEST",                      JavaType.value_translator( "int" ).literal( "400" ) );
         fields.put( "SC_CONFLICT",                         JavaType.value_translator( "int" ).literal( "409" ) );
         fields.put( "SC_CONTINUE",                         JavaType.value_translator( "int" ).literal( "100" ) );
         fields.put( "SC_CREATED",                          JavaType.value_translator( "int" ).literal( "201" ) );
         fields.put( "SC_EXPECTATION_FAILED",               JavaType.value_translator( "int" ).literal( "417" ) );
         fields.put( "SC_FORBIDDEN",                        JavaType.value_translator( "int" ).literal( "403" ) );
         fields.put( "SC_FOUND",                            JavaType.value_translator( "int" ).literal( "302" ) );
         fields.put( "SC_GATEWAY_TIMEOUT",                  JavaType.value_translator( "int" ).literal( "504" ) );
         fields.put( "SC_GONE",                             JavaType.value_translator( "int" ).literal( "410" ) );
         fields.put( "SC_HTTP_VERSION_NOT_SUPPORTED",       JavaType.value_translator( "int" ).literal( "505" ) );
         fields.put( "SC_INTERNAL_SERVER_ERROR",            JavaType.value_translator( "int" ).literal( "500" ) );
         fields.put( "SC_LENGTH_REQUIRED",                  JavaType.value_translator( "int" ).literal( "411" ) );
         fields.put( "SC_METHOD_NOT_ALLOWED",               JavaType.value_translator( "int" ).literal( "405" ) );
         fields.put( "SC_MOVED_PERMANENTLY",                JavaType.value_translator( "int" ).literal( "301" ) );
         fields.put( "SC_MOVED_TEMPORARILY",                JavaType.value_translator( "int" ).literal( "302" ) );
         fields.put( "SC_MULTIPLE_CHOICES",                 JavaType.value_translator( "int" ).literal( "300" ) );
         fields.put( "SC_NO_CONTENT",                       JavaType.value_translator( "int" ).literal( "204" ) );
         fields.put( "SC_NON_AUTHORITATIVE_INFORMATION",    JavaType.value_translator( "int" ).literal( "203" ) );
         fields.put( "SC_NOT_ACCEPTABLE",                   JavaType.value_translator( "int" ).literal( "406" ) );
         fields.put( "SC_NOT_FOUND",                        JavaType.value_translator( "int" ).literal( "404" ) );
         fields.put( "SC_NOT_IMPLEMENTED",                  JavaType.value_translator( "int" ).literal( "501" ) );
         fields.put( "SC_NOT_MODIFIED",                     JavaType.value_translator( "int" ).literal( "304" ) );
         fields.put( "SC_OK",                               JavaType.value_translator( "int" ).literal( "200" ) );
         fields.put( "SC_PARTIAL_CONTENT",                  JavaType.value_translator( "int" ).literal( "206" ) );
         fields.put( "SC_PAYMENT_REQUIRED",                 JavaType.value_translator( "int" ).literal( "402" ) );
         fields.put( "SC_PRECONDITION_FAILED",              JavaType.value_translator( "int" ).literal( "412" ) );
         fields.put( "SC_PROXY_AUTHENTICATION_REQUIRED",    JavaType.value_translator( "int" ).literal( "407" ) );
         fields.put( "SC_REQUEST_ENTITY_TOO_LARGE",         JavaType.value_translator( "int" ).literal( "413" ) );
         fields.put( "SC_REQUEST_TIMEOUT",                  JavaType.value_translator( "int" ).literal( "408" ) );
         fields.put( "SC_REQUEST_URI_TOO_LONG",             JavaType.value_translator( "int" ).literal( "414" ) );
         fields.put( "SC_REQUESTED_RANGE_NOT_SATISFIABLE",  JavaType.value_translator( "int" ).literal( "416" ) );
         fields.put( "SC_RESET_CONTENT",                    JavaType.value_translator( "int" ).literal( "205" ) );
         fields.put( "SC_SEE_OTHER",                        JavaType.value_translator( "int" ).literal( "303" ) );
         fields.put( "SC_SERVICE_UNAVAILABLE",              JavaType.value_translator( "int" ).literal( "503" ) );
         fields.put( "SC_SWITCHING_PROTOCOLS",              JavaType.value_translator( "int" ).literal( "101" ) );
         fields.put( "SC_TEMPORARY_REDIRECT",               JavaType.value_translator( "int" ).literal( "307" ) );
         fields.put( "SC_UNAUTHORIZED",                     JavaType.value_translator( "int" ).literal( "401" ) );
         fields.put( "SC_UNSUPPORTED_MEDIA_TYPE",           JavaType.value_translator( "int" ).literal( "415" ) );
         fields.put( "SC_USE_PROXY",                        JavaType.value_translator( "int" ).literal( "305" ) );
      }
      catch( TranslatorException e ) {
         System.err.println( "Ups - none string translator ? " + e );
      }
   }

   public String name() {
      return "javax.servlet.http.HttpServletResponse";
   }

   public String php_name() {
      return "none direct equivalent";
   }

}

/**
 * @composed 1 - 1 HttpServletResponseValueTranslator
 */
class HttpServletResponseTranslator extends StaticHttpServletResponseTranslator implements ITypeTranslator {

   private static HttpServletResponseValueTranslator value_translator = new HttpServletResponseValueTranslator();

   static {
      ITypeTranslator tr = new HttpServletResponseTranslator();
      JavaType.add_type_translator( tr );
   }

   private HttpServletResponseTranslator() {}

   public IValueTranslator value_translator() {
      return value_translator;
   }
}

class HttpServletResponseValueTranslator extends StaticHttpServletResponseTranslator implements IValueTranslator {

   JavaType myJavaType() throws TranslatorException {
      return JavaType.value_type( "javax.servlet.http.HttpServletResponse" );
   }

   public Translation translate_addHeader(Translation ... args) throws TranslatorException {
      makeConsistent( args, new int[] {1, 2} );
      return translate("addHeader", "header(%2$s . \": \" . %3$s, false)", 2, JavaType.voidType(), new JavaType[] { JavaType.string(), JavaType.string() }, true, args, true);
   }

   public Translation translate_addIntHeader(Translation ... args) throws TranslatorException {
      makeConsistent( args, new int[] {1, 2} );
      return translate("addIntHeader", "header(%2$s . \": \" . %3$s, false)", 2, JavaType.voidType(), new JavaType[] { JavaType.string(), JavaType.integer() }, true, args, false);
   }

   public Translation translate_flushBuffer(Translation ... args) throws TranslatorException {
      return translate("flushBuffer", "ob_end_flush()", 0, JavaType.voidType(), null, true, args, false);
   }

   public Translation translate_getBufferSize(Translation ... args) throws TranslatorException {
      return translate("getBufferSize", "(int) ob_get_length()", 0, JavaType.integer(), null, false, args, true);
   }

   public Translation translate_isCommitted(Translation ... args) throws TranslatorException {
      return translate("isCommitted", "headers_sent()", 0, JavaType.bool(), null, true, args, true);
   }

   public Translation translate_reset(Translation ... args) throws TranslatorException {
      return translate("reset", "{ ob_end_clean(); ob_start(); }", 0, JavaType.voidType(), null, false, args, false);
   }

   public Translation translate_resetBuffer(Translation ... args) throws TranslatorException {
      return translate("resetBuffer", "{ ob_end_clean(); ob_start(); }", 0, JavaType.voidType(), null, false, args, false);
   }

   public Translation translate_sendError(Translation ... args) throws TranslatorException {
      makeConsistent(args, new int[] {1, 2});

      if( args.length == 2 ) {
         return translate("sendError", "header(\"HTTP/1.1 \" . %2$s)", JavaType.voidType(), JavaType.integer(), true, args, false);
      }

      return translate("sendError",
                       "{ header(\"HTTP/1.1 \" . %2$s); header(\"Content-type: text/html\"); echo \"<html><header/><body>\" . %3$s . \"</body></html>\"; }",
                       2, JavaType.voidType(), new JavaType[] { JavaType.integer(), JavaType.string() }, true, args, false);
   }

   public Translation translate_setStatus(Translation ... args) throws TranslatorException {
      makeConsistent(args, new int[] {1, 2});

      if( args.length == 2 ) {
         return translate("setStatus", "header(\"HTTP/1.1 \" . %2$s)", JavaType.voidType(), JavaType.integer(), true, args, false);
      }

      return translate("setStatus", "header(\"HTTP/1.1 \" . %2$s . \" \" . %3$s)", 2, JavaType.voidType(), new JavaType[] { JavaType.integer(), JavaType.string() }, true, args, false);
   }

   public Translation translate_setBufferSize(Translation ... args) throws TranslatorException {
      return translate("setBufferSize", "ini_set(\"output_buffering\", %2$s)", JavaType.voidType(), JavaType.integer(), true, args, false);
   }

   public Translation translate_setContentLength(Translation ... args) throws TranslatorException {
      makeConsistent(args, 1);
      return translate("setContentLength", "header(\"Content-Length: \" . %2$s)", JavaType.voidType(), JavaType.integer(), true, args, false);
   }

   public Translation translate_setContentType(Translation ... args) throws TranslatorException {
      makeConsistent(args, 1);
      return translate("setContentType", "header(\"Content-Type: \" . %2$s)", JavaType.voidType(), JavaType.string(), true, args, false);
   }

   public Translation translate_setHeader(Translation ... args) throws TranslatorException {
      makeConsistent( args, new int[] {1, 2} );
      return translate("setHeader", "header(%2$s . \": \" . %3$s, false)", 2, JavaType.voidType(), new JavaType[] { JavaType.string(), JavaType.string() }, true, args, true);
   }

   public Translation translate_setIntHeader(Translation ... args) throws TranslatorException {
      makeConsistent( args, new int[] {1, 2} );
      return translate("setIntHeader", "header(%2$s . \": \" . %3$s, false)", 2, JavaType.voidType(), new JavaType[] { JavaType.string(), JavaType.integer() }, true, args, false);
   }
}
